package cn.qlu.yhy.service.aop;


import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.apache.log4j.Logger;

import cn.qlu.yhy.service.impl.UserServiceImpl;
import cn.qlu.yhy.utils.AppContext;

public class LogMethodTime implements MethodInterceptor {

    private final Logger logger = Logger.getLogger(UserServiceImpl.class);

    @Override
    public Object invoke(MethodInvocation invocation) throws Throwable {

        long startTime = System.currentTimeMillis();

        Object returnValue = invocation.proceed();
        String methodName = invocation.getMethod().getName();

        long endTime = System.currentTimeMillis();

        StringBuilder sb = new StringBuilder();

        String userName = AppContext.getAppContext().getUserName();
        String className = invocation.getMethod().getDeclaringClass().getSimpleName();
        sb.append(userName + " : " + className + " : " + methodName + " : " + (endTime - startTime));

        logger.info(sb.toString());
        //        System.out.println(sb.toString());
        return returnValue;
    }

}
